package org.dualr.litelog.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.dualr.litelog.entity.Category;

public class CategoryDao {

	private static CategoryDao _instance = null;

	public static CategoryDao getInstance() {
		if (_instance == null) {
			_instance = new CategoryDao();
		}
		return _instance;
	}

	// insert
	public String insert(Category category) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(category);
		} finally {
			pm.close();
		}
		return category.getId().toString();
	}

	// delete
	public void deleteById(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Category category = pm.getObjectById(Category.class, id);
		pm.deletePersistent(category);
	}

	// get all category
	@SuppressWarnings("unchecked")
	public List<Category> getAll() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Category.class);
		List<Category> category = null;
		try {
			category = (List<Category>) query.execute();
			return category;
		} finally {
			query.closeAll();
		}
	}

	// get category list by property

	@SuppressWarnings("unchecked")
	public List<Category> getListByProperty(String property, String value,
			String propertyType) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Category.class);
		String filter = property + " == propertyPrama";
		String param = null;
		if (propertyType.equals("string")) {
			param = "String propertyPrama";
		} else {
			param = "Long propertyPrama";
		}
		query.setFilter(filter);
		query.declareParameters(param);
		List<Category> category = null;
		try {
			category = (List<Category>) query.execute(value);
			return category;
		} finally {
			query.closeAll();
		}
	}

	// get category by id
	public Category getById(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Category.class);
		Category category = null;
		category = (Category) query.execute(id);
		return category;
	}

	// get category name by id
	public String getNameById(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Category category = pm.getObjectById(Category.class, id);
		pm.close();
		return category.getName();
	}

	// update category
	public void updateCategory(Category category) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Category c = pm.getObjectById(Category.class, category.getId());
			c.setName(category.getName());
			c.setParentId(category.getParentId());
			c.setType(category.getType());
			c.setCounts(category.getCounts());
		} finally {
			pm.close();
		}
	}

	// update category counts
	public void updateCounts(long id, String type) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Category category = pm.getObjectById(Category.class, id);
		try {
			if (type.equals("increment")) {
				category.setCounts(category.getCounts() + 1);
			} else {
				category.setCounts(category.getCounts() - 1);
			}
		} finally {
			pm.close();
		}
	}
}
